package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.dao.ProjectInfoDao;
import io.renren.dto.ProjectInfoDto;
import io.renren.entity.ProjectInfoEntity;
import io.renren.entity.RichtextContentEntity;
import io.renren.service.ProjectInfoService;
import io.renren.util.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("projectInfoService")
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoDao, ProjectInfoEntity> implements ProjectInfoService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper<ProjectInfoEntity>();
        wrapper.eq("enabled", 0);
        wrapper.orderByDesc("id");
        IPage<RichtextContentEntity> page = this.page(
                new Query<ProjectInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 公司官网查询内容列表
     *
     * @param params
     * @return 返回列表数据
     */
    public Map<String, Object> querList(Map<String, Object> params) {
        Map<String, Object> newParam=new HashMap<>();
        if(params.get("limit")!=null){
            newParam.put("limit",params.get("limit"));
        }
        if(params.get("page")!=null){
            newParam.put("page",params.get("page"));
        }

        PageUtils pageUtils = queryPage(newParam);
        Map<String, Object> map = new HashMap<>();
        if (pageUtils != null) {
            map.put("totalCount", pageUtils.getTotalCount());
            map.put("currPage", pageUtils.getCurrPage());
            map.put("totalPage", pageUtils.getTotalPage());
            if (pageUtils.getList() != null && !pageUtils.getList().isEmpty()) {
                List<ProjectInfoDto> projectInfoDtos = new ArrayList<>();
                for (int index = 0; index < pageUtils.getList().size(); index++) {
                    ProjectInfoEntity entity = (ProjectInfoEntity) pageUtils.getList().get(index);
                    ProjectInfoDto dto = new ProjectInfoDto();
                    dto.setId(entity.getId());
                    dto.setCompanyName(entity.getCompanyName());
                    dto.setProjectName(entity.getProjectName());
                    dto.setEndTime(entity.getEndTime());
                    dto.setPublishedTime(entity.getPublishedTime());
                    projectInfoDtos.add(dto);
                }
                map.put("list", projectInfoDtos);
            }
        }
        return map;
    }

}
